package utils;

import com.aspose.words.*;
import lombok.extern.slf4j.Slf4j;

import java.io.*;

/**
 * @author lwq
 * @ClassName AsposeUtil.java
 * @Description //TODO $
 * @createTime 2023-06-06 16:31
 */
@Slf4j
public class AsposeUtil {

    public byte[] asposeWord2Pdf(InputStream inputStream) {

        if (!getLicense()) { // 验证License 若不验证则转化出的pdf文档会有水印产生
            return null;
        }
        if (inputStream.equals(null)) {
            log.info("word为null");
            return null;
        }

        try {
            long old = System.currentTimeMillis();
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            Document doc = new Document(inputStream); //Address是将要被转化的word文档
            doc.save(os, SaveFormat.PDF);//全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换
            os.close();
            log.info("word2pdf消耗" + (System.currentTimeMillis() - old) + "毫秒");
            return os.toByteArray();
        } catch (Exception e) {
            log.info("doc转pdf文件失败，", e);
            return null;
        }
    }

    public static boolean getLicense() {
        boolean result = false;
        try {
            InputStream resourceAsStream = AsposeUtil.class.getClassLoader().getResourceAsStream("wordPath/license.xml");
            License aposeLic = new License();
            aposeLic.setLicense(resourceAsStream);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }


    public static boolean doc2pdf(String inPath, String outPath) {
        setFontsSources();

        if (!getLicense()) { // 验证License 若不验证则转化出的pdf文档会有水印产生
            return false;
        }
        FileOutputStream os = null;
        try {
            long old = System.currentTimeMillis();
            File file = new File(outPath); // 新建一个空白pdf文档
            os = new FileOutputStream(file);
            Document doc = new Document(inPath); // Address是将要被转化的word文档
          //  doc.setFontSettings(new FontSettings());
          //  doc.getFontSettings().setFontsSources(new FontSourceBase[]{new SystemFontSource(), new FolderFontSource("/Users/c7/Library/Fonts", true)});

            doc.save(os, SaveFormat.PDF);// 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF,
            // EPUB, XPS, SWF 相互转换
            long now = System.currentTimeMillis();
            System.out.println("pdf转换成功，共耗时：" + ((now - old) / 1000.0) + "秒"); // 转化用时
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }finally {
            if (os != null) {
                try {
                    os.flush();
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return true;
    }

    /**
     * 设置字体资源
     * 不设置会导致word转pdf的时候字体丢失
     */
    public static void setFontsSources() {
        String fontsDir = "/Users/c7/Library/Fonts";

        //将用户目录字体添加到字体源中
        FontSourceBase[] originalFontSources = FontSettings.getDefaultInstance().getFontsSources();
        FolderFontSource folderFontSource = new FolderFontSource(fontsDir, true);

        FontSourceBase[] updatedFontSources = {originalFontSources[0], folderFontSource};
        FontSettings.getDefaultInstance().setFontsSources(updatedFontSources);
    }

    public static void main(String[] args) {
        doc2pdf("/Users/c7/Desktop/测评表.docx","/Users/c7/Desktop/测评表.pdf");
    }

}
